<template>
  <!--头部区域-->
  <el-container class="home-container">
    <el-header>
      <div class="image">
        <img src="../assets/logo.png" width="150px" height="50px" />
        <el-radio-group @click="toggleCollapse" style="padding: 0 25px;">
          <span v-if="!isCollapse" class="iconfont icon-shouqi"></span>
          <span v-if="isCollapse" class="iconfont icon-zhankai"></span>
        </el-radio-group>
      </div>

      <div class="user">
        <span>欢迎你，马维东</span>
        <el-button size="mini" type="info" @click="logout">退出</el-button>
      </div>
    </el-header>
    <!-- 页面主题区域 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <el-menu
          background-color="#304156"
          text-color="#BFCBD9"
          active-text-color="#409EFF"
          unique-opened
          :collapse="isCollapse"
          :collapse-transition="false"
          router
          :default-active="activePath"
        >
          <!-- 一级菜单 -->
          <el-submenu
            :index="item.id + ''"
            v-for="item in menulist"
            :key="item.id"
          >
            <template slot="title">
              <i :class="iconsObj[item.id]"></i>
              <span>{{ item.authName }}</span>
            </template>
            <!-- 二級菜單 -->
            <el-menu-item
              :index="'/' + subItem.path"
              v-for="subItem in item.children"
              :key="subItem.id"
              @click="saveNavState('/' + subItem.path)"
            >
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{ subItem.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右侧内容主体 -->
      <el-main>
        <!-- 路由占位符 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      menulist: [],
      iconsObj: {
        '125': 'iconfont icon-icon_user',
        '103': 'iconfont icon-rising',
        '101': 'iconfont icon-rising',
        '102': 'iconfont icon-rising',
        '145': 'iconfont icon-rising'
      },
      isCollapse: false,
      activePath: ''
    }
  },
  created() {
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    logout() {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    async getMenuList() {
      const { data: res } = await this.$http.get('menu/list')
      if (res.ret !== 1) return this.$message.error(res.msg)
      this.menulist = res.data
    },
    toggleCollapse() {
      this.isCollapse = !this.isCollapse
    },
    saveNavState(activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    }
  }
}
</script>

<style lang="less" scoped>
.el-header {
  background-color: #304156;
  display: flex;
  justify-content: space-between;
  padding-left: 0%;
  align-items: center;
  color: #fff;
  font-size: 14px;
  .image {
    display: flex;
    align-items: center;
    flex-grow: 23.6;
    img {
      padding-left: 20px;
    }
    span {
      display: inline-block;
      vertical-align: middle;
      font-size: 25px;
    }
  }

  .user {
    flex-grow: 0.4;
    display: flex;
    justify-content: space-between;
    align-items: center;
    span {
      font-size: 14px;
      border-right: none;
      font-family: Helvetica Neue, Helvetica, PingFang SC, Hiragino Sans GB,
        Microsoft YaHei, Arial, sans-serif;
      text-rendering: optimizeLegibility;
      -webkit-font-smoothing: antialiased;
    }
  }
}
.el-aside {
  background-color: #304156;
  .el-menu {
    border-right: none;
    font-family: Helvetica Neue, Helvetica, PingFang SC, Hiragino Sans GB,
      Microsoft YaHei, Arial, sans-serif;
    text-rendering: optimizeLegibility;
    -webkit-font-smoothing: antialiased;
  }
}
.el-main {
  position: relative;
  > el-menu {
    position: absolute;
    width: 100%;
    margin: 0px;
    padding: 0px;
    border: 0px;
  }
  background-color: #fff;
}
.home-container {
  height: 100%;
}
.iconfont {
  margin-right: 10px;
}

</style>
